package com.mes.basicdata.manager;

import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.model.DefaultUser;
import com.bstek.bdf2.core.model.UserDept;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.mes.basicdata.dao.iface.IDeptDao;
import com.mes.basicdata.dao.iface.IEmployeeDao;
import com.mes.basicdata.domain.Dept;
import com.mes.basicdata.domain.Employee;
import com.mes.basicdata.manager.iface.IEmployeeManager;
import com.mes.common.manager.BasicManager;

@Component
public class EmployeeManager extends BasicManager<Employee> implements IEmployeeManager{

	@Autowired
	private IEmployeeDao employeeDao;
	
	@Autowired
	private IDeptDao deptDao;
	
	@Override
	public void resetPassword(String employeeId) throws Exception{

		Employee employee = employeeDao.getById(employeeId);
		if (employee != null) {
			SecureRandom random = new SecureRandom();
			byte[] salt = new byte[12];
			random.nextBytes(salt);
			// Get a MessageDigest object

			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(salt);
			//md.update(ObjectConstants.DEFAULT_PASSWORD.getBytes("UTF8"));
			byte[] digest = md.digest();
			byte[] pwd = new byte[28];
			System.arraycopy(salt, 0, pwd, 0, 12);
			System.arraycopy(digest, 0, pwd, 12, 16);
			
			employee.setPassword(pwd.toString());
			employeeDao.update(employee);
			
		}
	}
	
	
	@Override
	public void testTransaction() throws Exception{
		
		Dept dept = new Dept();
		dept.setDeptUid("dai");
		dept.setDeptName("dai");
		deptDao.add(dept);
		
		Employee employee = this.getById("402881e95a9aa267015a9aa36aa00000");
		employee.setEmployeeUid(null);
		employee.setEmployeeName("test");
		employeeDao.add(employee);
		
	}
	
	@Override
	public void saveEmployee(Map<String, Collection<Employee>> dataItems) throws Exception {
		for (String key : dataItems.keySet()){
			Collection<Employee> employees = dataItems.get(key);
			
			for (Employee employee : employees) {
				DefaultUser user = employee.getBDFUser();
				
				switch (EntityUtils.getState(employee)) {
				case MODIFIED:
					if (user != null){
						user.setMale((employee.getSex() == 1) ? true : false);
						user.setCname(employee.getEmployeeName());
					}
					
					UserDept userDept1 = employeeDao.getUserDeptByUser(employee.getEmployeeUid());
					if (userDept1 != null){
						userDept1.setDeptId(employee.getDeptUid());
						
					}
					
					employeeDao.update(employee);
					employeeDao.updateUser(user);
					employeeDao.updateUserDept(userDept1);
					
					break;
				case DELETED:
					employeeDao.delete(employee);
					employeeDao.deleteUser(user);
					
					break;
				case NEW:
					if (user != null){
						user.setPassword("36a310a727eb2c9c4bcb5561d8aa6d9b77a25ab6");//设置默认密码为666666
						user.setSalt("4");
						user.setMale((employee.getSex() == 1) ? true : false);
						user.setCname(employee.getEmployeeName());
						user.setAdministrator(false);
						user.setEnabled(true);
						user.setCompanyId("111");
					}
					
					UserDept userDept = new UserDept();
					
					userDept.setId(employee.getEmployeeUid() + "/" + employee.getDeptUid());
					userDept.setDeptId(employee.getDeptUid());
					userDept.setUsername(employee.getEmployeeUid());
					
					employeeDao.add(employee);
					employeeDao.saveUser(user);
					employeeDao.saveUserDept(userDept);
					break;
				default:
					break;
				}
			}
		}
	}
}
